home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zfunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.6 KB  |  264 lines

  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zfunc.c,v 1.6 2000/09/19 19:00:54 lpd Exp $ */
  20. /* Generic PostScript language interface to Functions */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gscdefs.h"
  25. #include "gsfunc.h"
  26. #include "gsstruct.h"
  27. #include "ialloc.h"
  28. #include "idict.h"
  29. #include "idparam.h"
  30. #include "ifunc.h"
  31. #include "store.h"
  32.  
  33. /* Define the maximum depth of nesting of subsidiary functions. */
  34. #define MAX_SUB_FUNCTION_DEPTH 3
  35.  
  36. /* GC descriptors */
  37. gs_private_st_ptr(st_function_ptr, gs_function_t *, "gs_function_t *",
  38.           function_ptr_enum_ptrs, function_ptr_reloc_ptrs);
  39. gs_private_st_element(st_function_ptr_element, gs_function_t *,
  40.               "gs_function_t *[]", function_ptr_element_enum_ptrs,
  41.               function_ptr_element_reloc_ptrs, st_function_ptr);
  42.  
  43. /* ------ Operators ------ */
  44.  
  45. private int zexecfunction(P1(i_ctx_t *));
  46.  
  47. /* <dict> .buildfunction <function_struct> */
  48. private int
  49. zbuildfunction(i_ctx_t *i_ctx_p)
  50. {
  51.     os_ptr op = osp;
  52.     gs_function_t *pfn;
  53.     ref cref;            /* closure */
  54.     int code;
  55.  
  56.     code = ialloc_ref_array(&cref, a_executable | a_execute, 2,
  57.                 ".buildfunction");
  58.     if (code < 0)
  59.     return code;
  60.     code = fn_build_function(op, &pfn, imemory);
  61.     if (code < 0) {
  62.     ifree_ref_array(&cref, ".buildfunction");
  63.     return code;
  64.     }
  65.     make_istruct_new(cref.value.refs, a_executable | a_execute, pfn);
  66.     make_oper_new(cref.value.refs + 1, 0, zexecfunction);
  67.     ref_assign(op, &cref);
  68.     return 0;
  69. }
  70.  
  71. /* <in1> ... <function_struct> %execfunction <out1> ... */
  72. private int
  73. zexecfunction(i_ctx_t *i_ctx_p)
  74. {
  75.     os_ptr op = osp;
  76.  
  77.     /*
  78.      * Since this operator's name begins with %, the name is not defined
  79.      * in systemdict.  The only place this operator can ever appear is
  80.      * in the execute-only closure created by .buildfunction.
  81.      * Therefore, in principle it is unnecessary to check the argument.
  82.      * However, we do a little checking anyway just on general
  83.      * principles.  Note that since the argument may be an instance of
  84.      * any subclass of gs_function_t, we currently have no way to check
  85.      * its type.
  86.      */
  87.     if (!r_is_struct(op) ||
  88.     !r_has_masked_attrs(op, a_executable | a_execute, a_executable | a_all)
  89.     )
  90.     return_error(e_typecheck);
  91.     {
  92.     gs_function_t *pfn = (gs_function_t *) op->value.pstruct;
  93.     int m = pfn->params.m, n = pfn->params.n;
  94.     int diff = n - (m + 1);
  95.  
  96.     if (diff > 0)
  97.         check_ostack(diff);
  98.     {
  99.         float params[20];    /* arbitrary size, just to avoid allocs */
  100.         float *in;
  101.         float *out;
  102.         int code = 0;
  103.  
  104.         if (m + n <= countof(params)) {
  105.         in = params;
  106.         } else {
  107.         in = (float *)ialloc_byte_array(m + n, sizeof(float),
  108.                         "%execfunction(in/out)");
  109.         if (in == 0)
  110.             code = gs_note_error(e_VMerror);
  111.         }
  112.         out = in + m;
  113.         if (code < 0 ||
  114.         (code = float_params(op - 1, m, in)) < 0 ||
  115.         (code = gs_function_evaluate(pfn, in, out)) < 0
  116.         )
  117.         DO_NOTHING;
  118.         else {
  119.         if (diff > 0)
  120.             push(diff);    /* can't fail */
  121.         else if (diff < 0) {
  122.             pop(-diff);
  123.             op = osp;
  124.         }
  125.         code = make_floats(op + 1 - n, out, n);
  126.         }
  127.         if (in != params)
  128.         ifree_object(in, "%execfunction(in)");
  129.         return code;
  130.     }
  131.     }
  132. }
  133.  
  134. /* ------ Procedures ------ */
  135.  
  136. /* Build a function structure from a PostScript dictionary. */
  137. int
  138. fn_build_function(const ref * op, gs_function_t ** ppfn, gs_memory_t *mem)
  139. {
  140.     return fn_build_sub_function(op, ppfn, 0, mem);
  141. }
  142. int
  143. fn_build_sub_function(const ref * op, gs_function_t ** ppfn, int depth,
  144.               gs_memory_t *mem)
  145. {
  146.     int code, type, i;
  147.     gs_function_params_t params;
  148.  
  149.     if (depth > MAX_SUB_FUNCTION_DEPTH)
  150.     return_error(e_limitcheck);
  151.     check_type(*op, t_dictionary);
  152.     code = dict_int_param(op, "FunctionType", 0, max_int, -1, &type);
  153.     if (code < 0)
  154.     return code;
  155.     for (i = 0; i < build_function_type_table_count; ++i)
  156.     if (build_function_type_table[i].type == type)
  157.         break;
  158.     if (i == build_function_type_table_count)
  159.     return_error(e_rangecheck);
  160.     /* Collect parameters common to all function types. */
  161.     params.Domain = 0;
  162.     params.Range = 0;
  163.     code = fn_build_float_array(op, "Domain", true, true, ¶ms.Domain, mem);
  164.     if (code < 0)
  165.     goto fail;
  166.     params.m = code >> 1;
  167.     code = fn_build_float_array(op, "Range", false, true, ¶ms.Range, mem);
  168.     if (code < 0)
  169.     goto fail;
  170.     params.n = code >> 1;
  171.     /* Finish building the function. */
  172.     /* If this fails, it will free all the parameters. */
  173.     return (*build_function_type_table[i].proc)
  174.     (op, ¶ms, depth + 1, ppfn, mem);
  175. fail:
  176.     gs_free_const_object(mem, params.Range, "Range");
  177.     gs_free_const_object(mem, params.Domain, "Domain");
  178.     return code;
  179. }
  180.  
  181. /* Allocate an array of function objects. */
  182. int
  183. alloc_function_array(uint count, gs_function_t *** pFunctions,
  184.              gs_memory_t *mem)
  185. {
  186.     gs_function_t **ptr;
  187.  
  188.     if (count == 0)
  189.     return_error(e_rangecheck);
  190.     ptr = gs_alloc_struct_array(mem, count, gs_function_t *,
  191.                 &st_function_ptr_element, "Functions");
  192.     if (ptr == 0)
  193.     return_error(e_VMerror);
  194.     memset(ptr, 0, sizeof(*ptr) * count);
  195.     *pFunctions = ptr;
  196.     return 0;
  197. }
  198.  
  199. /*
  200.  * Collect a heap-allocated array of floats.  If the key is missing, set
  201.  * *pparray = 0 and return 0; otherwise set *pparray and return the number
  202.  * of elements.  Note that 0-length arrays are acceptable, so if the value
  203.  * returned is 0, the caller must check whether *pparray == 0.
  204.  */
  205. int
  206. fn_build_float_array(const ref * op, const char *kstr, bool required,
  207.              bool even, const float **pparray, gs_memory_t *mem)
  208. {
  209.     ref *par;
  210.     int code;
  211.  
  212.     *pparray = 0;
  213.     if (dict_find_string(op, kstr, &par) <= 0)
  214.     return (required ? gs_note_error(e_rangecheck) : 0);
  215.     if (!r_is_array(par))
  216.     return_error(e_typecheck);
  217.     {
  218.     uint size = r_size(par);
  219.     float *ptr = (float *)
  220.         gs_alloc_byte_array(mem, size, sizeof(float), kstr);
  221.  
  222.     if (ptr == 0)
  223.         return_error(e_VMerror);
  224.     code = dict_float_array_check_param(op, kstr, size, ptr, NULL,
  225.                         0, e_rangecheck);
  226.     if (code < 0 || (even && (code & 1) != 0)) {
  227.         gs_free_object(mem, ptr, kstr);
  228.         return(code < 0 ? code : gs_note_error(e_rangecheck));
  229.     }
  230.     *pparray = ptr;
  231.     }
  232.     return code;
  233. }
  234.  
  235. /*
  236.  * If a PostScript object is a Function procedure, return the function
  237.  * object, otherwise return 0.
  238.  */
  239. gs_function_t *
  240. ref_function(const ref *op)
  241. {
  242.     if (r_has_type(op, t_array) &&
  243.     r_has_masked_attrs(op, a_executable | a_execute,
  244.                a_executable | a_all) &&
  245.     r_size(op) == 2 &&
  246.     r_has_type_attrs(op->value.refs + 1, t_operator, a_executable) &&
  247.     op->value.refs[1].value.opproc == zexecfunction &&
  248.     r_is_struct(op->value.refs) &&
  249.     r_has_masked_attrs(op->value.refs, a_executable | a_execute,
  250.                a_executable | a_all)
  251.     )
  252.     return (gs_function_t *)op->value.refs->value.pstruct;
  253.     return 0;
  254. }
  255.  
  256. /* ------ Initialization procedure ------ */
  257.  
  258. const op_def zfunc_op_defs[] =
  259. {
  260.     {"1.buildfunction", zbuildfunction},
  261.     {"1%execfunction", zexecfunction},
  262.     op_def_end(0)
  263. };
  264.